package com.beiing.tianshuai.tianshuai.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**
 * Created by Jinni on 2017/8/1 0001.
 */

public class SideLitterBarView extends View {

    /*
     * 默认的导航栏中字母的大小
     */
    private static final int DEFAULT_ALPHA_SIZE = 12;

    /*
     * 字母导航栏选择时，在中部显示所选择字母的TextView
     */
    private TextView mDialog;

    /*
     * 字母列表内容
     */
    private static final String[] ALPHA_LIST = {"#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    /*
     * 画笔
     */
    private Paint mPaint;

    /*
     * 默认字母颜色
     */
    private static final int DEFAULT_CHARACTER = Color.parseColor("#FFFFFF");

    /*
     * 选中字母颜色
     */
    private static final int SELECTED_CHARACTER = Color.parseColor("#67A0FF");

    /*
     * 条目高度
     */
    private int itemHeight;

    /*
     *手指按下的字母改变接口
     */
    private onWordsChangeListener listener;

    /*
     *手指按下的字母索引
     */
    private int touchIndex = 0;

    public void setListener(onWordsChangeListener listener) {
        this.listener = listener;
    }

    public SideLitterBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 抗锯齿，字体圆滑
        mPaint.setColor(DEFAULT_CHARACTER); // 设置字体颜色
        mPaint.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL)); // 设置字体为粗体
        mPaint.setTextSize(sp2px(DEFAULT_ALPHA_SIZE));
    }

    public int sp2px(float spValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 获取整个SideBar的宽和高
        int width = getWidth();
        int height = getHeight() - getPaddingTop() - getPaddingBottom();

        // 平均每行所分配到的高度值
        itemHeight = height / ALPHA_LIST.length;

        for (int i = 0; i < ALPHA_LIST.length; i++) {
            // 计算每个字母的x坐标
            float xPos = (width - mPaint.measureText(ALPHA_LIST[i])) / 2;
            // 计算每个字母的y坐标（顶部预留一个singleHeight的高度）
            float yPos = itemHeight * (i + 1) + getPaddingTop();

            // 如果当前字母是点击的字母，则改变画笔颜色
            if (i == touchIndex) {
                mPaint.setColor(SELECTED_CHARACTER);
            }

            // 绘制文本
            canvas.drawText(ALPHA_LIST[i], xPos, yPos, mPaint);

            // 绘制完成后，将画笔颜色修改为默认的颜色
            mPaint.setColor(DEFAULT_CHARACTER);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                float y = event.getY();
                //获得我们按下的是哪一个索引
                int index = (int) (y / itemHeight);
                if (index != touchIndex) {
                    touchIndex = index;
                }
                //防止数组越界
                if (listener != null && 0 <= touchIndex && touchIndex <= ALPHA_LIST.length - 1) {
                    listener.wordsChange(ALPHA_LIST[touchIndex]);
                }
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                //手指抬起不做任何操作
                break;
        }
        return true;
    }

    public void setTouchIndex(String word) {
        for (int i = 0; i < ALPHA_LIST.length; i++) {
            if (ALPHA_LIST[i].equals(word)) {
                touchIndex = i;
                invalidate();
                return;
            }
        }
    }

    public interface onWordsChangeListener {
        void wordsChange(String words);
    }
}